﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_settlement.Outputs
{
    public class TMS_OutputRepository : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_OutputRepository
    {
        public TMS_OutputRepository(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<int> CreateInput(TMS_Output tMS_Output)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"insert into TMS_Output values(NEWID(),'{tMS_Output.Output_Company}','{tMS_Output.Output_InvoiceType}','{tMS_Output.Output_InvoiceAmount}','{tMS_Output.Output_Rate}','{tMS_Output.Output_Tax}','{tMS_Output.Output_Date}','{tMS_Output.Output_Note}','{tMS_Output.Output_MakingPeople}',1,GETDATE())", null, await GetDbTransactionAsync());
        }

        public async Task<int> DeleteInput(string id)
        {
            var dbConnection = await GetDbConnectionAsync();
            var count = 0;
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                var sql = $"update TMS_Output set Output_Delete='false' where Id='{item}'";
                count += await dbConnection.ExecuteAsync(sql, null,
                 await GetDbTransactionAsync());
            }
            return count;
        }

        public async Task<PageResponse<TMS_OutputDto>> GetInput(string name, string date, bool? state, int pageindex, int pagesize)
        {
            PageResponse<TMS_OutputDto> response = new PageResponse<TMS_OutputDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select a.*,b.Employees_Name as name from  TMS_Output a left join TMS_Employees b on a.Output_MakingPeople=b.Id where a.Output_Delete='true'";
            var list = await dbConnection.QueryAsync<TMS_OutputDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageTotal = list.Count();
            if (name != null)
            {
                sql += $" and a.Output_Company = '{name}'";
            }
            if (date != null)
            {
                sql += $" and a.Output_Date = '{date}'";
            }
            if (state != null)
            {
                sql += $" and a.Output_InvoiceType = '{state}'";
            }
            sql += $" order by a.Output_DateTime offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await dbConnection.QueryAsync<TMS_OutputDto>(sql, transaction: await GetDbTransactionAsync());
            response.PageData = lists.ToList();
            return response;
        }

        public async Task<int> UpdateInput(TMS_Output tMS_Output)
        {
            throw new NotImplementedException();
        }
    }
}
